<template>
  <div class="index-register">
    <el-row :gutter="config.rowGutter">
      <el-col :xs="config.colSpanXs" :lg="config.colSpanLg">
        <el-form ref="registerFrom" :rules="rules" :model="registerFrom" :label-width="config.fromLabelWidth">
          <el-form-item :label="username.label" prop="username">
            <el-input v-model="registerFrom.username"></el-input>
          </el-form-item>
          <el-form-item :label="nickname.label" prop="nickname">
            <el-input v-model="registerFrom.nickname"></el-input>
          </el-form-item>
          <el-form-item :label="password.label" prop="password">
            <el-input type="password" v-model="registerFrom.password"></el-input>
          </el-form-item>
          <el-form-item :label="rePassword.label" prop="rePassword">
            <el-input type="password" v-model="registerFrom.rePassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('registerFrom')">注册</el-button>
            <el-button @click="resetForm('registerFrom')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import returnCode from '../../const/returnCode'
  export default {
    name: 'indexRegister',
    data: function () {
      let rePasswordCheck = (rule, value, callback) => {
        if (value !== this.registerFrom.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      let checkUserIsExist = async (rule, value, callback) => {
        let res = await this.$http.post('/user/check-user-is-exist', this.registerFrom)
        // console.log(res)
        if (parseInt(res.data.status) === returnCode.error) {
          callback(new Error('用户名已经存在!'))
        } else {
          callback()
        }
      }
      return {
        registerFrom: {
          username: '',
          nickname: '',
          password: '',
          rePassword: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, message: '长度在至少3个字符', trigger: 'blur' },
            { validator: checkUserIsExist, trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '请输入昵称', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 4, message: '长度在至少4个字符', trigger: 'blur' }
          ],
          rePassword: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: rePasswordCheck, trigger: 'blur' }
          ]
        },
        username: {
          label: '用户名',
          name: 'username',
          placeholder: '请输入用户名'
        },
        nickname: {
          label: '昵称',
          name: 'nickname',
          placeholder: '请输入昵称'
        },
        password: {
          label: '密码',
          name: 'password',
          placeholder: '请输入密码'
        },
        rePassword: {
          label: '再次输入密码',
          name: 'rePassword',
          placeholder: '请再次输入密码'
        },
        config: {
          colSpanXs: {span: 24},
          colSpanLg: {span: 12, offset: 6},
          rowGutter: 20,
          fromLabelWidth: '120px'
        }
      }
    },
    methods: {
      onSubmit: function (formName) {
        let _this = this
        this.$refs[formName].validate(function (valid) {
          if (valid) {
            _this.$http.post('/user/add-user', _this.registerFrom).then(res => {
              console.log(res)
              alert('success')
            }).catch(res => {

            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      resetForm: function (formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>

<style>
.index-register{
  margin-top: 20px;
}
</style>


